package com.lkd.web.controller;


import com.lkd.entity.Users;
import com.lkd.mapper.UsersMapper;
import com.lkd.service.IUserservice;
import com.lkd.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserservice userService;

    @Autowired
    private UsersMapper usersMapper;
    //    @RequestMapping("/login")

    //GET 的实现
    @GetMapping ("/login/{userId}/{userName}")
    public Users login(@PathVariable("userId") int userId, @PathVariable("userName") String userName){
        Users users=new Users();
        users.setUserId(userId);
        users.setUserName(userName);

        Users u2=userService.login2(users);

        if (Objects.isNull(u2)){
            System.out.println("登陆失败");
        }else {
            System.out.println("登陆成功");
        }

        System.out.println(users);
        System.out.println(u2);

        return u2;
        //http://localhost:8080/user/login/2/瑞儿
    }

    //POST 的实现
    @PostMapping("/login3")
    public Users login(@RequestBody Users arg){
        // 从数据库里面去验证前段项目传过来的账号信息是否存在？
        // controller(控制层)->service（业务层）->mapper（数据持久层）
        Users users = userService.login2(arg);
        if (Objects.isNull(users)){
            System.out.println("登录失败");
        }else {
            System.out.println("登录成功");
        }

        return users; // 最终@RestController注解会把该方法的返回值转换成json对象
    }

    @PostMapping("/login4")
    public ResponseResult login2(@RequestBody Users arg){
        // 从数据库里面去验证前段项目传过来的账号信息是否存在？
        // controller(控制层)->service（业务层）->mapper（数据持久层）
        Users users = userService.login(arg);
        ResponseResult result= null;
        if (Objects.isNull(users)){
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "登录失败");
            System.out.println("登录失败");
        }else {
            result = new ResponseResult<>(HttpStatus.OK.value(), "登录成功", users);
            System.out.println("登录成功");
        }
        return result; // 最终@RestController注解会把该方法的返回值转换成json对象
    }




    //以下是已经实现的增删改查，不过没有用老师的方法
    @Autowired
    JdbcTemplate jdbcTemplate;

//查
    @GetMapping("/userList")
    public List<Map<String, Object>> userList(){
        String sql="select * from newshixi.users";
        List<Map<String,Object>> list_maps=jdbcTemplate.queryForList(sql);
        return list_maps;
        //http://localhost:8080/user/userList
    }

//增
    @GetMapping("/addUser")
    public String addUser(){
        String sql="insert into newshixi.users(userId,userName,accountId,accountPwd,gender,birthday,phone) " +
                "values(6,\"舒蕊瑞\",\"006\",\"123\",\"女\",\"2001-06-01T00:00:00\",\"11812\")";
        jdbcTemplate.update(sql);
        return "add-ok";
        //http://localhost:8080/user/addUser
        //(数据库中userId等不能有重复的)
    }

    /*
    * /{accountId}/{accountPwd}/{birthday}/{phone}
    * , @PathVariable("accountId") String accountId, @PathVariable("accountPwd") String accountPwd
            , @PathVariable("birthday") String birthday
            , @PathVariable("phone") String phone
    * */
//改  (根据userId改userName和gender)
    @GetMapping("updateUser/{userId}/{userName}/{gender}")
    public String updateUser(@PathVariable("userId") int userId, @PathVariable("userName") String userName
            , @PathVariable("gender") String gender){

        String sql="update newshixi.users set  userName=?,gender=?  where  userId="+userId;

        //封装Object
        Object[] objects = new Object[2];
        objects[0]=userName;
        objects[1]=gender;

        jdbcTemplate.update(sql,objects);
        return "update-ok";
        //http://localhost:8080/user/updateUser/6/狗狗呀/男
    }

//删
    @GetMapping("/deleteUser/{userId}")
    public String deleteUser(@PathVariable("userId") int userId){
        String sql="delete from newshixi.users where userId=?";
        jdbcTemplate.update(sql,userId);
        return "delete-ok";
        //http://localhost:8080/user/deleteUser/6
    }


}
